package com.hq.shop06back.controller;

import com.hq.commons.common.R;
import com.hq.shop06back.feign.api.GoodsService;
import com.hq.shop06back.vo.GoodsType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/goodsTypeController")
public class GoodsTypeController {

    @Autowired
    private GoodsService goodsService;

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/10 9:10
     * @description: 跳转网页方法
     */
    @GetMapping("{page}.html")
    public String toPage(@PathVariable String page, Integer id, ModelMap modelMap) {
        modelMap.put("id", id);
        return "goodsType/" + page;
    }

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/10 9:11
     * @description: 查询商品数据方法
     */
    @GetMapping("getGoodsList")
    public String getGoodsList(ModelMap modelMap) {
        List<GoodsType> goodsTypeList = goodsService.getGoodsList();
        modelMap.put("goodsTypeList", goodsTypeList);
        return "goodsType/goodsList";
    }

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/10 9:10
     * @description: 树节点查询节点方法
     */
    @RequestMapping("getGoodsTypeTree")
    @ResponseBody
    public R getGoodsTypeTree() {
//        创建一个list，用来储存所有的节点
        List<Map<String, Object>> nodeList = new ArrayList<>();
//        遍历集合
        List<GoodsType> goodsList = goodsService.getGoodsList();
        for (GoodsType goodsType : goodsList) {
//        创建一个map用来储存节点的信息
            Map<String, Object> node = new HashMap<>();
            node.put("id", goodsType.getId());
            node.put("name", goodsType.getGoodsTypeName());
            node.put("pid", goodsType.getGoodsTypePid());
//        添加到list中
            nodeList.add(node);
        }
        return R.success(nodeList);
    }

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/10 9:12
     * @description: 修改节点方法
     */
    @PostMapping("updateGoodsType")
    @ResponseBody
    public R updateGoodsType(GoodsType goodsType) {
        return R.write(goodsService.updateGoodsType(goodsType));
    }

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/10 9:12
     * @description: 删除节点方法
     */
    @GetMapping("deleteGoodsType/{id}")
    @ResponseBody
    public R deleteGoodsType(@PathVariable Integer id) {
        return R.write(goodsService.deleteGoodsType(id));
    }

    /**
     * @author: HQ
     * @return:
     * @time: 2021/7/10 14:37
     * @description: 添加节点
     */
    @PostMapping("addGoods")
    @ResponseBody
    public R addGoods(GoodsType goodsType) {
        boolean tar = goodsService.addGoods(goodsType);
        return R.success();
    }

    @GetMapping("getGoodsTypeListByPid/{id}")
    @ResponseBody
    public List<GoodsType> getGoodsTypeListByPid(@PathVariable Integer id) {
        return (goodsService.getGoodsTypeListByPid(id));
    }
}
